﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.Providers;

namespace MethodWorx.CMS.Administration
{
    public class ApplicationTypeService
    {
        private IApplicationSettingsRepository settings;

        public ApplicationTypeService(IApplicationSettingsRepository settings)
        {
            this.settings = settings;
        }

        public ApplicationTypeService(ICmsDataProvider provider)
            : this(provider.GetService<IApplicationSettingsRepository>())
        
        
        {
        }

        public string GetBaseUrl()
        {
            var settings = this.settings.FindSettings();
            if (settings != null)
            {
                string baseUrl = settings.BaseUri.TrimEnd('/') + "/";
                return baseUrl;
            }
            else
                return "";
        }

        public string GetPagePreviewUrl(Page page)
        {
            var settings = this.settings.FindSettings();
            if (settings != null)
            {
                string baseUrl = settings.BaseUri.TrimEnd('/') + "/";
                //  ok, get the application type
                foreach (ApplicationType type in ApplicationTypeFactory.Instance.GetApplicationTypes())
                {
                    if (type.ApplicationTypeId == settings.ApplicationTypeId)
                        baseUrl += type.GetPreviewUrl(page);
                }

                return baseUrl;
            }
            else
                return "";
        }

        public IApplicationSettingsRepository ApplicationSettingsRepository
        {
            get { return this.settings; }
        }
    }
}
